package com.shawn.example.mvvmdemo.home.ui.wx

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.jetpack.mvvm.lib.base.BaseFragment
import com.jetpack.mvvm.lib.base.extention.extraFragment
import com.jetpack.mvvm.lib.binding.ext.viewBind
import com.shawn.example.mvvmdemo.home.R
import com.shawn.example.mvvmdemo.home.databinding.FragmentWxDetailArticleBinding
import com.shawn.example.mvvmdemo.home.viewmodel.WXViewModel
import com.shawn.example.mvvmdemo.home.viewmodel.WxDetailViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class WxDetailArticleFragment private constructor() : BaseFragment() {
    companion object {
        private const val TAG = "WxDetailArticleFragment"
        private const val KEY_PARAM_ID = "key_param_id"
        private const val KEY_PARAM_AUTHOR = "key_param_author"
        fun getInstance(id: Int, author: String): WxDetailArticleFragment {
            val fragment = WxDetailArticleFragment()
            val args = Bundle()
            args.putInt(KEY_PARAM_ID, id);
            args.putString(KEY_PARAM_AUTHOR, author)
            fragment.arguments = args
            return fragment
        }
    }

    private val authorId by extraFragment(KEY_PARAM_ID, 0)
    private val authorName by extraFragment<String>(KEY_PARAM_AUTHOR)
    private val binding by viewBind<FragmentWxDetailArticleBinding>()
    private lateinit var articleAdapter: ArticleListAdapter
    private var curPage = 1
    private val vm by viewModels<WxDetailViewModel>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }

    override fun initView() {
        articleAdapter = ArticleListAdapter(R.layout.item_search_pager, null)
        binding.wxArticlesListRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)
            adapter = articleAdapter
        }

    }

    override fun lazyLoad() {
        vm.getAuthorWxArticles(authorId, curPage)

    }

    override fun observeViewModel() {
        vm.detailLV.observe(this) {
            articleAdapter.setList(it)
        }
    }
}